package com.lanou3g.Hw0514.study;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UploadServlet",urlPatterns = "/upload")
//我们也可以通过加上MultipartConfig注解
//来获取表单中的 其他input
//建议在处理完上传之后,转发到一个单独的servlet去处理表单
//中其他的input,因为使用了该注解有可能导致
//FileUpLoad不好用
@MultipartConfig
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //上传
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取请求数据
        String code= request.getParameter("code");
        //响应将得到数据返回给前端
        response.getWriter().println(code);
         //
        String uploadPath=getServletContext().getRealPath("")+File.separator+"upload";
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()){
            //创建文件夹
            uploadDir.mkdirs();
        }

        ServletFileUpload upload = new ServletFileUpload();
        FileItemFactory factory = new DiskFileItemFactory();
        upload.setFileItemFactory(factory);

        try {
            List<FileItem> fileItems=upload.parseRequest(request);
            if (fileItems==null){
                return;
            }
            for (FileItem fileItem : fileItems) {
                if (fileItem.isFormField()){
//                    //不是一个文件,是一个表单数据
//                    //input中的name属性
//                    String fieldName=fileItem.getFieldName();
//                    //name是文件名,当不传文件时为null
//                    String name = fileItem.getName();
//                    //string是input的值
//                    //getString 参数传递  编码格式
//                    //解决乱码
//                    String s= fileItem.getString("utf-8");
//                    System.out.println("fileName:"+fieldName);
//                    System.out.println("name:"+name);
//                    System.out.println("s:"+s);
                    continue;
                }
                String fileName= fileItem.getName();

                //处理后的文件名
                String afterName= TestUUID.handleFileName(fileName);
                //创建文件壳子
                File file = new File(uploadDir,afterName);
                fileItem.write(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
